package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseMapper;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.entity.Community;
import com.atguigu.mapper.CommunityMapper;
import com.atguigu.mapper.HouseMapper;
import com.atguigu.service.CommunityService;

import javax.annotation.Resource;
import java.util.List;

@Service(interfaceClass = CommunityService.class)
public class CommunityServiceImpl extends BaseServiceImpl<Community> implements CommunityService {
    @Resource
    private HouseMapper houseMapper;

    @Resource
    private CommunityMapper communityMapper;
    @Override
    public BaseMapper<Community> getEntityMapper() {
        return communityMapper;
    }

    @Override
    public void delete(Long id) {
        Integer count = houseMapper.findCountByCommunityId(id);
        if (count>0){
            //说明该小区下有房源，不可删除
            throw new RuntimeException("该小区下有房源，不可删除！");
        }
        //没有房源 可以删除
        super.delete(id);
    }

    /**
     * 查询所有小区
     * @return
     */
    @Override
    public List<Community> findAll() {
        return communityMapper.findAll();
    }
}
